﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using SomethingSpacial.CustomControls;
using SomethingSpacial.Presenters;
using SomethingSpacial.ViewModel;

namespace SomethingSpacial.Views.Default
{
    public partial class DefaultView : UserControl
    {
        public DefaultView()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(DefaultView_Loaded);
        }

        void DefaultView_Loaded(object sender, RoutedEventArgs e)
        { 
            SetLoginState();

            FeedPresenter NewFeedPresenter = new FeedPresenter();
            NewFeedPresenter.PresentMainFeed(FeedList);

            NewsItem.FeedAddress = DefaultViewModel.NewsFeed;

            ((DefaultViewModel)this.DataContext).loadUserGroups();
        }

        public void SetLoginState()
        {
            if (App.CurrentUser.IsLoggedIn)
            {
                LoginLink.Text = DefaultViewModel.LogoutText;
            }
            else
            {
                LoginLink.Text = DefaultViewModel.LoginText;
            }
        }

        private void ContentPanel_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            ContentPanel.RoleForward();
        }

        private void Login_MouseEnter(object sender, MouseEventArgs e)
        {
            ((TextBlock)sender).Foreground = new SolidColorBrush(Tools.GetColor(DefaultViewModel.ForegroundColor));
            ((TextBlock)sender).TextDecorations = TextDecorations.Underline;
        }

        private void Login_MouseLeave(object sender, MouseEventArgs e)
        {
            ((TextBlock)sender).Foreground = new SolidColorBrush(Colors.Black);
            ((TextBlock)sender).TextDecorations = null;
        }

        private void Login_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            ContentTarget.Children.Add(new LoginForm()); 
        }

        private void LayoutRoot_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            ContentPanel.IsInit = false;
            ContentPanel.InvalidateMeasure();
        }

        private void FullScreen_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (Application.Current.Host.Content.IsFullScreen)
            {
                Application.Current.Host.Content.IsFullScreen = false;
            }
            else
            {
                Application.Current.Host.Content.IsFullScreen = true;
            } 
        }

        private void FeedSlideTitle_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            System.Windows.Browser.HtmlPage.Window.Navigate(new Uri(DefaultViewModel.NewsFeed), "_blank");
        }

        private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        { 
            System.Windows.Browser.HtmlPage.Window.Navigate(new Uri(((Image)sender).Tag.ToString()), "_blank");
        }  
    }
}
